package com.qs.imp;

import java.sql.Connection; 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import com.opensymphony.xwork2.ActionContext;
import com.qs.dao.ConnectionManager;
import com.qs.entity.Message;
import com.qs.entity.MoonMessage;
import com.qs.entity.UserFriend;
import com.qs.inter.MessageInt;

public class MessageImp implements MessageInt {
	Connection conn;
	PreparedStatement ps;
	ResultSet rs;
	
	public int countNews(int friendUserId) throws Exception {
		int countNews = 0;
		String sql = "select count(MoonId) from MoonInfo where UserId=?";
		conn =ConnectionManager.getConnction();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, friendUserId);
			rs = ps.executeQuery();
			rs.next();
			countNews = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return countNews;
	}
	

	public List smessage(int friendUserId) throws Exception {
		List list = new ArrayList<UserFriend>();
		String sql = "select * from Messageboard where userId=?";
		conn = ConnectionManager.getConnction();
		ps = conn.prepareStatement(sql);
		ps.setInt(1, friendUserId);
		rs = ps.executeQuery();
		
		while(rs.next()){
			Message message1 = new Message();
			message1.setUserId(rs.getInt("userId"));
			message1.setFriendName(rs.getString("friendName"));
			message1.setFriendMessage(rs.getString("friendMessage"));
			message1.setFriendMessageTime(rs.getDate("friendMessageTime"));
			message1.setMessageId(rs.getInt("messageId"));
			list.add(message1);
		}
		
		return list;
	}


	public List smessagereply(int messageId) throws Exception {
		List list = new ArrayList<Message>();
		conn = ConnectionManager.getConnction();
		String sql = "select * from MessageboardReply where messageId = ?";
		ps = conn.prepareStatement(sql);
		ps.setInt(1, messageId);
		rs = ps.executeQuery();
		while(rs.next()){
			Message message1 = new Message();
			message1.setMessageId(rs.getInt("messageId"));
			message1.setUserReply(rs.getString("userReply"));
			message1.setUserReplyTime(rs.getDate("userReplyTime"));
			message1.setFriendName(rs.getString("friendName"));
			list.add(message1);
		}
		return list;	
	}


	public int amessage(Message message) throws Exception {
		conn = ConnectionManager.getConnction();
		String sql = "insert into Messageboard(userId,friendMessage,friendMessageTime,friendName) values(?,?,?,?)";
		ps = conn.prepareStatement(sql);
		ps.setInt(1, (Integer)ActionContext.getContext().getSession().get("FRIENDID"));
		ps.setString(2, message.getFriendMessage());
		ps.setDate(3,  new java.sql.Date(new java.util.Date().getTime()));
		ps.setString(4, (String)ActionContext.getContext().getSession().get("UserName"));
		int i = ps.executeUpdate();
		return i;
	}


	public int amessagereply(Message message) throws Exception {
		conn = ConnectionManager.getConnction();
		String sql = "insert into messageboardReply(messageId,userReply,userReplyTime,friendName) values(?,?,?,?)";
		ps = conn.prepareStatement(sql);
		ps.setInt(1, message.getMessageId());
		ps.setString(2, message.getUserReply());
		ps.setDate(3,new java.sql.Date(new java.util.Date().getTime()));
		ps.setString(4, (String)ActionContext.getContext().getSession().get("UserName"));
		int i = ps.executeUpdate();
		
		return i;
	}

}
